<template>
  <div>
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" />
    <div class="editinfo">
      <van-image :src="userInfo.photo" fit="cover" round />
      <!-- 图片上传 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell
      @click="clickCurren"
      title="名称"
      is-link
      :value="userInfo.name"
    />
    <van-cell
      @click="clickGender"
      title="性别"
      is-link
      :value="userInfo.gender ? '男' : '女'"
    />
    <van-cell
      @click="clickBirthday"
      title="生日"
      is-link
      :value="userInfo.birthday"
    />

    <!-- 修改昵称弹出窗口 -->
    <van-dialog
      @cancel="currenShow = false"
      @confirm="saveName"
      :before-close="beforeClose"
      v-model="currenShow"
      title="修改昵称"
      show-cancel-button
    >
      <van-cell-group>
        <van-field ref="fieldName" v-model="currenName" class="filed-item" />
      </van-cell-group>
    </van-dialog>

    <!-- 修改性别弹出窗口 -->
    <van-action-sheet
      v-model="genderShow"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @cancel="genderShow = false"
      @select="genderSelect"
      class="gender-item"
    />

    <!-- 修改生日弹出窗口 -->
    <van-popup v-model="birthdayShow" round position="bottom">
      <van-datetime-picker
        @cancel="birthdayShow = false"
        @confirm="saveBirthday"
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>

    <!-- 图片裁剪 -->
    <div v-show="cropShow" class="crop-box">
      <vueCropper
        ref="cropper"
        :img="cropImg"
        autoCrop
        autoCropWidth="200"
        autoCropHeight="200"
      ></vueCropper>
      <van-button @click="editPhoto" type="primary">裁剪</van-button>
      <van-button @click="cropShow = false" style="right: 0" type="primary"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { VueCropper } from 'vue-cropper'
import { changeInfoAPI, editPhotoAPI } from '@/api/user'
import dayjs from 'dayjs'
export default {
  components: {
    VueCropper
  },
  data() {
    return {
      canClose: true,
      cropShow: false,
      birthdayShow: false,
      currenShow: false,
      currenName: '',
      genderShow: false,
      actions: [
        { name: '男', value: 1 },
        { name: '女', value: 0 }
      ],
      minDate: new Date('1949-10-1'),
      maxDate: new Date('2030-10-1'),
      currentDate: new Date(),
      cropImg: ''
    }
  },
  methods: {
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      this.cropImg = file.content
      this.cropShow = true
    },
    editPhoto() {
      this.$refs.cropper.getCropBlob(async data => {
        console.log(data)
        const fm = new FormData()
        fm.append('photo', data)
        const res = await editPhotoAPI(fm)
        console.log(res)
        this.$store.commit('changeInfo', {
          ...this.userInfo,
          photo: res.data.photo
        })
        this.$toast('修改头像成功')
        this.cropShow = false
      })
    },
    async clickCurren() {
      this.currenShow = true
      this.currenName = this.userInfo.name
      await this.$nextTick()
      this.$refs.fieldName.focus()
    },
    async saveName() {
      if (this.currenName == '') {
        this.canClose = false
        return this.$toast('请输入你要修改的名字')
      }
      await changeInfoAPI({
        name: this.currenName
      })
      this.$store.commit('changeInfo', {
        ...this.userInfo,
        name: this.currenName
      })
      this.$toast('修改成功')
      this.currenShow = false
    },
    beforeClose(action, done) {
      done(this.canClose)
      this.canClose = true
    },
    clickGender() {
      this.genderShow = true
    },
    async genderSelect(action, index) {
      await changeInfoAPI({
        gender: this.actions[index].value
      })
      this.$store.commit('changeInfo', {
        ...this.userInfo,
        gender: this.actions[index].value
      })
      this.$toast('修改成功')
      this.genderShow = false
    },
    clickBirthday() {
      this.birthdayShow = true
      this.currentDate = new Date(this.userInfo.birthday)
    },
    async saveBirthday() {
      const birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
      await changeInfoAPI({
        birthday
      })
      this.$store.commit('changeInfo', {
        ...this.userInfo,
        birthday
      })
      this.$toast('修改成功')
      this.birthdayShow = false
    }
  },
  computed: {
    userInfo() {
      return this.$store.state.userinfo
    }
  },
  created() {
    this.$store.dispatch('reqUserInfo')
  }
}
</script>

<style lang="less" scoped>
.editinfo {
  height: 140px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  .van-image {
    height: 120px;
    width: 120px;
  }
  .van-uploader {
    position: absolute;
    left: 0;
    top: 0;
    ::v-deep .van-uploader__upload {
      position: absolute;
      left: 0;
      top: 0;
      width: 100vw;
      height: 140px;
      opacity: 0;
    }
  }
}
.filed-item {
  border-bottom: 1px solid #ddd;
}
.van-popup {
  height: 220px;
}
.gender-item {
  height: 160px;
}
.crop-box {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  .van-button {
    position: absolute;
    bottom: 0;
  }
}
</style>
